package com.atguigu.gmall.order.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.order.model.OrderInfo;
import com.atguigu.gmall.order.service.OrderInfoService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.junit.jupiter.api.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author: atguigu
 * @create: 2023-01-12 10:21
 */
@RestController
@RequestMapping("/api/order")
public class OrderApiController {


    @Autowired
    private OrderInfoService orderInfoService;


    /**
     * 订单确认页面数据模型汇总
     *
     * @param request
     * @return
     */
    @GetMapping("/auth/trade")
    public Result<Map<String, Object>> trade(HttpServletRequest request) {
        //1.获取用户ID
        String userId = AuthContextHolder.getUserId(request);
        System.out.println("订单微服务，获取从web-all传输用户ID：" + userId);
        //2.业务逻辑汇总订单确认页面数据
        Map<String, Object> map = orderInfoService.trade(userId);
        return Result.ok(map);
    }


    /**
     * 订单提交-保存
     *
     * @param orderInfo 订单信息
     * @return
     */
    @PostMapping("/auth/submitOrder")
    public Result<Long> submitOrder(@RequestBody OrderInfo orderInfo, HttpServletRequest request) {
        //设置用户ID
        String userId = AuthContextHolder.getUserId(request);
        orderInfo.setUserId(Long.valueOf(userId));
        //获取流水号
        String tradeNo = request.getParameter("tradeNo");
        //调用业务逻辑

        Long orderId = orderInfoService.submitOrder(orderInfo, tradeNo);
        return Result.ok(orderId);
    }

    /**
     * 查询我的订单列表
     *
     * @param page    页码
     * @param limit   页大小
     * @param request 请求对象
     * @return
     */
    @GetMapping("/auth/{page}/{limit}")
    public Result getOrder(@PathVariable Long page,
                           @PathVariable Long limit,
                           HttpServletRequest request) {
        //1.获取登录用户
        String userId = AuthContextHolder.getUserId(request);
        String status = request.getParameter("status");
        //2.封装分页对象
        IPage<Order> orderPage = new Page<>(page, limit);
        //3.调用业务逻辑
        orderPage = orderInfoService.getOrder(orderPage, userId, status);
        return Result.ok(orderPage);
    }


    /**
     * 根据订单ID查询订单信息
     *
     * @param orderId 订单ID
     * @return
     */
    @GetMapping("/inner/getOrderInfo/{orderId}")
    public Result getOrderInfo(@PathVariable("orderId") Long orderId) {
        OrderInfo orderInfo = orderInfoService.getOrderInfo(orderId);
        return Result.ok(orderInfo);
    }


    /**
     * 库存系统发起拆单请求
     * 参数采用表单方式提交
     *
     * @param orderId       "1"
     * @param wareSkuMapStr "[{"wareId":"1","skuIds":["26"]}]"
     * @return
     */
    @PostMapping("/orderSplit")
    public String orderSplit(@RequestParam("orderId") Long orderId, @RequestParam("wareSkuMap") String wareSkuMapStr) {
        return orderInfoService.orderSplit(orderId, wareSkuMapStr);
    }


    /**
     * 秒杀提交订单，秒杀订单不需要做前置判断，直接下单
     *
     * @param orderInfo
     * @return
     */
    @PostMapping("inner/seckill/submitOrder")
    public Long submitSeckillOrder(@RequestBody OrderInfo orderInfo) {
        return orderInfoService.submitSeckillOrder(orderInfo);
    }
}
